﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using IndieStudio.BugsBang.Game;
using IndieStudio.BugsBang.Utility;

///Developed by Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

[CustomEditor (typeof(Path))]
public class PathEditor : Editor {

	public override void OnInspectorGUI ()
	{
		SerializedObject attrib = new SerializedObject (target);
		Path path = (Path)target;

		attrib.Update ();

		EditorGUILayout.Separator ();
		#if !(UNITY_5 || UNITY_2017 || UNITY_2018_0 || UNITY_2018_1 || UNITY_2018_2)
			//Unity 2018.3 or higher
			EditorGUILayout.BeginHorizontal();
			GUI.backgroundColor = Colors.cyanColor;
			EditorGUILayout.Separator();
			if(PrefabUtility.GetCorrespondingObjectFromSource(path.gameObject)!=null)
			if (GUILayout.Button("Apply", GUILayout.Width(70), GUILayout.Height(30), GUILayout.ExpandWidth(false)))
			{
				PrefabUtility.ApplyPrefabInstance(path.gameObject, InteractionMode.AutomatedAction);
			}
			GUI.backgroundColor = Colors.whiteColor;
			EditorGUILayout.EndHorizontal();
		#endif
		EditorGUILayout.Separator ();
		
		EditorGUILayout.Separator ();
		EditorGUILayout.HelpBox ("Click on 'Add New Point' button to add new Point", MessageType.Info);
		EditorGUILayout.HelpBox ("Click on 'Remove Last Point' button to remove the lastest point in the list", MessageType.Info);
		EditorGUILayout.HelpBox ("Move the Points to set up the path", MessageType.Info);
		EditorGUILayout.Separator ();

		path.pathColor = EditorGUILayout.ColorField ("Path Color",path.pathColor);
		EditorGUILayout.Separator ();

		GUILayout.BeginHorizontal ();
		GUI.backgroundColor = Colors.greenColor;         

		if (GUILayout.Button ("Add New Point", GUILayout.Width (110), GUILayout.Height (20))) {
			path.CreateNewPoint (Vector3.zero);
		}

		GUI.backgroundColor = Colors.redColor;         
		if (GUILayout.Button ("Remove Last Point", GUILayout.Width (150), GUILayout.Height (20))) {
			if (path.points.Count != 0) {
				if(path.points[path.points.Count - 1]!=null)
					DestroyImmediate (path.points[path.points.Count - 1].gameObject);
				path.points.RemoveAt (path.points.Count - 1);
			}
		}

		GUI.backgroundColor = Colors.whiteColor;
		GUILayout.EndHorizontal ();

		EditorGUILayout.Separator ();

		path.showContents = EditorGUILayout.Foldout (path.showContents, "Points");
		if (path.showContents) {
		for (int i = 0; i <  path.points.Count; i++) {
				EditorGUILayout.Separator ();
				path.points [i] = EditorGUILayout.ObjectField ("Point ["+i+"]",  path.points [i], typeof(Transform), true) as Transform;
				EditorGUILayout.Separator ();
				GUILayout.Box ("", GUILayout.ExpandWidth (true), GUILayout.Height (2));
			}
		}

		EditorGUILayout.Separator ();
		EditorGUILayout.HelpBox ("Add the objects you want to move in the path to the list below", MessageType.Info);
		EditorGUILayout.Separator ();
		EditorGUILayout.PropertyField(attrib.FindProperty("objectsScale"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("delayBeforeInsantiate"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("waveDelayTime"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("waveItemDelayTime"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("objectsSpeed"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("objectsAnimatorSpeed"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("instantiateMode"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("trailEffect"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("pathObjects"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("objectsScale"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("runOnStart"), true);
		EditorGUILayout.PropertyField (attrib.FindProperty ("destoryObjectsOnReachEnd"), true);
		attrib.ApplyModifiedProperties ();

		if (GUI.changed) {
			DirtyUtil.MarkSceneDirty ();
		}
	}
}